package com.dure.hotmemory.controllers;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dure.commonlib.util.AssetsUtil;
import com.dure.commonlib.util.StreamUtil;
import com.dure.hotmemory.R;
import com.dure.hotmemory.adapters.CodeDetailAdapter;
import com.dure.hotmemory.bean.Code;
import com.dure.hotmemory.bean.Planet;
import com.dure.hotmemory.listeners.ConfirmListener;
import com.dure.hotmemory.scenes.BaseScene;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by leix on 30/05/2017.
 */

public class BianMaController extends Controller {
    public BianMaController(BaseScene baseScene) {
        super(baseScene);
    }

    public List<Code> getCodeList() {

        InputStream is = AssetsUtil.getAssets(activity, "number_code.json");
        Gson gson = new Gson();
        String codeJsonStr = StreamUtil.inputStream2String(is);
        Type datasetListType = new TypeToken<Collection<Code>>() {}.getType();
        List<Code> codeArrayList = gson.fromJson(codeJsonStr, datasetListType);

        return codeArrayList;
    }

    public void showCodeDetailDialog(Code code, final ConfirmListener confirmListener){
        final Dialog dialog = new Dialog(activity, R.style.customDialog);
        View view = LayoutInflater.from(activity).inflate(R.layout.code_detail_dialog, null);
        TextView titleTV = (TextView) view.findViewById(R.id.title_tv);
        TextView closeTV = (TextView) view.findViewById(R.id.close_tv);
        TextView editTV = (TextView) view.findViewById(R.id.edit_tv);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);

        titleTV.setText("数字：" + code.number);

        closeTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });

        editTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
                if (confirmListener != null) {
                    confirmListener.confirmed(null);
                }
            }
        });

//        addCodeDetailItem(code, contentLL);

        CodeDetailAdapter adapter = new CodeDetailAdapter(activity);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(adapter = new CodeDetailAdapter(activity));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect,
                                       View view,
                                       RecyclerView parent,
                                       RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.left = 10;
                outRect.top = 10;
                outRect.right = 10;
                outRect.bottom = 20;
            }
        });

        adapter.setCodeList(getCodeListFromDB(code));


        dialog.setCanceledOnTouchOutside(false);
        WindowManager windowManager = activity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int width = (int) (display.getWidth() * 0.8f);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setContentView(view, params);

        dialog.show();
    }

    private void addCodeDetailItem(Code code, LinearLayout contentLL){
        List<Code> codeList = getCodeListFromDB(code);

        for (int i = 0; i < codeList.size(); i++) {
            Code codeItem = codeList.get(i);
            View view = LayoutInflater.from(activity).inflate(R.layout.code_detail, null);
            ImageView codeIV = (ImageView) view.findViewById(R.id.code_iv);
            TextView numberTV = (TextView) view.findViewById(R.id.number_tv);
            TextView prime_tv = (TextView) view.findViewById(R.id.prime_tv);
            TextView nameTV = (TextView) view.findViewById(R.id.name_tv);

            numberTV.setText(codeItem.number);
            nameTV.setText(codeItem.name);

            if (i == 0){
                prime_tv.setVisibility(View.GONE);
            }else{

            }

            Bitmap bitmap = BitmapFactory.decodeStream(AssetsUtil.getAssets(activity, codeItem.image));
            codeIV.setImageBitmap(bitmap);

            contentLL.addView(view);
        }

    }

    public List<Code> getCodeListFromDB(Code code){
        // fake data
        ArrayList<Code> codeArrayList = new ArrayList<>();
        for (int i = 0; i < 2; i++){
            Code codeItem = new Code();
            codeItem.name = code.name + i;
            codeItem.number = code.number;
            codeItem.image = code.image;
            codeArrayList.add(codeItem);
        }
//        codeArrayList.add(code);
//        codeArrayList.add(code);

        return codeArrayList;
    }
}
